package com.zhixinglu.spring.boot.mqtt.tio.listener;

import com.zhixinglu.spring.boot.mqtt.tio.utils.MqttUtil;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.spring.client.MqttClientCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 * MQTT客户端自定义配置扩展
 * </p>
 *
 * @author weixin
 * @date 2022/12/23 19:41
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class MqttClientCustomizerConfiguration {

    @Value("${mqtt.client.name}")
    private String name;

    @Value("${mqtt.client.username}")
    private String username;

    @Value("${mqtt.client.password}")
    private String password;

    @Value("${mqtt.client.ip}")
    private String ip;

    @Value("${mqtt.client.port}")
    private Integer port;

    @Bean
    public MqttClientCustomizer mqttClientCustomizer() {

        return creator -> {
            creator.clientId(MqttUtil.generateClientId())
                    .name(name)
                    .ip(ip)
                    .port(port)
                    .username(username)
                    .password(password);

            // 此处可自定义配置 creator，会覆盖 yml 中的配置
            log.info("mqtt client customizer Configuration, --- name: {} --- clientId: {} --- ip: {} , --- username: {} , --- password: {} ",
                    creator.getName(),
                    creator.getClientId(),
                    creator.getIp(),
                    creator.getUsername(),
                    creator.getPassword()
            );
        };
    }

}
